package com.asset.salesforce.validator;

import java.util.List;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import org.springframework.beans.factory.annotation.Autowired;

import com.asset.salesforce.domain.Lead;
import com.asset.salesforce.domain.User;
import com.asset.salesforce.service.LeadService;
import com.asset.salesforce.service.UserShiroService;

public class PhoneNotExistValidator implements ConstraintValidator<PhoneNotExist, Object> {

	@Autowired
	private LeadService leadService;
	@Autowired
	private UserShiroService service;
	@Override
	public void initialize(PhoneNotExist arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean isValid(Object object, ConstraintValidatorContext arg1) {
		User user = service.getCurrentUser();
		boolean success=false;
		List<Lead> leads = leadService.getLeads(user.getId());
		for (Lead lead : leads) {
			if(object.toString()!=null&&!object.toString().equalsIgnoreCase("")){
				if(lead.getPhone().equals(object.toString()))success= false;
				else{success=true;}
			}
			else{
				success= true;
			}
		}
		return success;
	}

}
